<?php

namespace Happy\CommandsService\RabbitMQ;

use PhpAmqpLib\Connection\AMQPStreamConnection;

class RabbitMqReciever extends BasicReciever
{
    protected array $callbacks = [];

    public function createConnection($host, $port, $user, $password)
    {
        $this->connection = new AMQPStreamConnection($host, $port, $user, $password);
        $this->channel = $this->connection->channel();
    }

    public function changeQueue(string $queueName)
    {
        $this->activeQueue = $queueName;
    }

    public function listen($callback)
    {
        $this->channel->basic_consume($this->activeQueue, '', false, true, false, false, $callback);
        while ($this->channel->is_open()) {
            $this->channel->wait(null, true);
        }
    }

    public function close()
    {
        $this->channel->close();
        $this->connection->close();
    }

    public function __destruct()
    {
        $this->channel->close();
        $this->connection->close();
    }
}
